package com.adtech.adms.commons.web.intercepter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 
 * 
 * @author Dormin 
 * @since 2011-5-17 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class SessionInterceptor extends AbstractInterceptor {


	private static final long serialVersionUID = 2892557153195245595L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		if(request.getSession()!=null) {
			String oldid = request.getSession().getId();
			request.getSession().invalidate();//清空session
			
			if(request.getCookies()!=null && request.getCookies().length>0) {
				Cookie cookie = request.getCookies()[0];//获取cookie
				cookie.setMaxAge(0);//让cookie过期
			}
			
//			for(Cookie cookie : request.getCookies()){			
//				cookie.setMaxAge(0);//让cookie过期
//			}
//			
			request.getSession(true);
			invocation.getInvocationContext().setSession(new SessionMap<String, Object>(request));
			String newid = request.getSession().getId();
		}

		return invocation.invoke();
	}

}
